package com.freewater.springboot.demo.aop.advice.generic;

import java.util.Collection;
import java.util.Objects;
import java.util.stream.Collectors;

public class UserService implements IService<User> {

    /**
     * 新增用户
     */
    public User addUser(User user) {
        System.out.printf("user added：%s%n", user);
        return user;
    }

    /**
     * 新增用户
     */
    public User addUser(String name, int age) {
        if (name == null) {
            throw new NullPointerException("name can not be null");
        }
        if (age < 0) {
            throw new NullPointerException("age can not be negative");
        }
        User user = new User();
        user.setName(name);
        user.setAge(age);
        System.out.printf("user added：%s%n", user);
        return user;
    }

    @Override
    public String describe(User user) {
        return user.toString();
    }

    @Override
    public String describeCollection(Collection<User> users) {
        return users.stream()
                .filter(Objects::nonNull).map(User::toString)
                .collect(Collectors.joining(","));
    }
}
